//
//  AppDelegate.m
//  UI2_UIButton
//
//  Created by Marry W. on 16/1/21.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = [[[UIViewController alloc] init] autorelease];
    
    // UIButton 按钮
    // 1.创建(便利构造器)
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    // 2.设置frame
    btn.frame = CGRectMake(100, 100, 100, 100);
    // 3.设置属性
    btn.backgroundColor = [UIColor redColor];
    // 4.绑定按钮点击事件(按钮被点击时 能够触发一个方法)
    // 参数1: target 目标(调用方法的人)
    // 参数2: action 动作(调用的方法)
    // 参数3: events 事件(方法的触发条件)
    // UIControlEventTouchUpInside 控制事件之 触摸顶端按下去
    // : 参数标志(之后一定会跟随一个参数)
    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    // 5.添加父视图
    [self.window addSubview:btn];
    // 按钮文字
    // 参数1: 标题内容
    // 参数2: 状态
    [btn setTitle:@"正常" forState:UIControlStateNormal];
    // 按钮长按就是高亮状态
    [btn setTitle:@"高亮" forState:UIControlStateHighlighted];
    // 高亮时 按钮显示触摸
    btn.showsTouchWhenHighlighted = YES;
    
    // 新建按钮 绑定click:方法
    UIButton *newBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    newBtn.frame = CGRectMake(250, 100, 100, 100);
    newBtn.backgroundColor = [UIColor yellowColor];
    [newBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:newBtn];
    
    btn.tag = 1000;
    newBtn.tag = 2000;
    
    
//    NSString *str = [NSArray array];
//    [str substringFromIndex:2];
    
    [_window release];
    return YES;
}

// 按钮点击时 触发的方法
//- (void)click:(id)sender
- (void)click:(UIButton *)sender
{
    sender.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
//    NSLog(@"点点点");
//    NSLog(@"%@", sender);
    if (sender.tag == 1000) {
        NSLog(@"左左左");
    }
    if (sender.tag == 2000) {
        NSLog(@"右右右");
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
